package top.deepdesigner.controller.market;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsKeyword;
import top.deepdesigner.service.DtsKeywordService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonListVO;

import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/22 星期四 22:35:01 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/keyword")
public class KeywordController {

  @Autowired
  private DtsKeywordService keywordService;

  @RequiresPermissionsDesc(menu = {"商场管理", "关键词"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:keyword:list"})
  @GetMapping("/list")
  public Object list(@RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "20") Integer limit,
                     @RequestParam(defaultValue = "add_time") String sort,
                     @RequestParam(defaultValue = "desc") String order,
                     @RequestParam(defaultValue = "") String keyword,
                     @RequestParam(defaultValue = "") String url) {
    CommonListVO<DtsKeyword> issueListVO = new CommonListVO<>();
    //获取分页数量
    Long count = keywordService.getKeywordCount();
    List<DtsKeyword> dtsKeywords = keywordService.getKeywordList(page, limit, sort, order, keyword, url);
    issueListVO.setTotal(count);
    issueListVO.setItems(dtsKeywords);
    return ResponseUtil.ok(issueListVO);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "关键词"}, button = "添加")
  @RequiresPermissions(value = {"admin:keyword:create"})
  @PostMapping("/create")
  public Object create(@RequestBody DtsKeyword keyword) {
    DtsKeyword dtsKeyword = keywordService.createKeyword(keyword);
    if (dtsKeyword == null) return ResponseUtil.fail(700, "关键字已存在");
    return ResponseUtil.ok(dtsKeyword);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "关键词"}, button = "修改")
  @RequiresPermissions(value = {"admin:keyword:update"})
  @PostMapping("/update")
  public Object update(@RequestBody DtsKeyword keyword) {
    DtsKeyword dtsKeyword = keywordService.updateKeyword(keyword);
    return ResponseUtil.ok(dtsKeyword);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "关键词"}, button = "删除")
  @RequiresPermissions(value = {"admin:keyword:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsKeyword keyword) {
    keywordService.deleteKeyword(keyword);
    return ResponseUtil.ok();
  }
}
